package mylittleinvestigations;

/**
 * Extends {@link InputStream} to allow the audio stream to be instantly closed
 * and reopened via the restart() method.
 *
 * @author dawnmew
 * @since 1.0
 *
 * Licensed under the MIT License.
 *
 * Copyright (c) 2012 dawnmew
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import org.dawnmew.jvirtualstream.ResettableInputStream;
import org.dawnmew.jvirtualstream.VirtualFileSystem;

public class SmartAudioInputStream extends InputStream
{

    String fileReference = null;
    AudioInputStream inputStream = null;
    VirtualFileSystem parent;

    public SmartAudioInputStream(String fileref, VirtualFileSystem parent)
    {
        this.parent = parent;
        this.fileReference = fileref;
        this.inputStream = getAudioInputStream(this.fileReference);
    }

    public boolean isValid()
    {
        return this.inputStream != null;
    }

    public AudioFormat getFormat()
    {
        return this.inputStream.getFormat();
    }

    @Override
    public int available() throws IOException
    {
        return this.inputStream.available();
    }

    @Override
    public int read() throws IOException
    {
        return this.inputStream.read();
    }

    @Override
    public int read(byte[] b) throws IOException
    {
        return this.inputStream.read(b);
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException
    {
        return this.inputStream.read(b, off, len);
    }

    @Override
    public void close() throws IOException
    {
        this.inputStream.close();
        this.inputStream = null;
    }

    public void restart()
    {
        try
        {
            this.inputStream.close();
        } catch (IOException ex)
        {
            Logger.getLogger(ResettableInputStream.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.inputStream = getAudioInputStream(this.fileReference);
    }

    private AudioInputStream getAudioInputStream(String path)
    {
        try
        {
            return AudioSystem.getAudioInputStream(new BufferedInputStream(parent.getInputStream(path)));
        } catch (Exception ex)
        {
            Logger.getLogger(SmartAudioInputStream.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}
